package com.bwie.rent.conf;

import com.alibaba.fastjson.JSON;
import com.bwie.rent.dao.RentDao;
import com.bwie.rent.entity.RentEntity;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName MyRunner
 * @Description TODO
 * @Author 李奉武
 * @Motto 逆水行舟, 不进则退
 * @Date 2022/3/22 21:41
 * @Version 1.0
 */
@Component
public class MyRunner implements ApplicationRunner {

    @Autowired
    RestHighLevelClient client;

    @Autowired
    RentDao rentDao;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        BulkRequest bulkRequest = new BulkRequest("house_rent");
        List<RentEntity> rentEntities = rentDao.selectList(null);
        for (RentEntity rentEntity : rentEntities) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(rentEntity.getHouseId().toString());
            String s = JSON.toJSONString(rentEntity);
            indexRequest.source(s, XContentType.JSON);
            bulkRequest.add(indexRequest);
        }
        client.bulk(bulkRequest, GuliESConfig.COMMON_OPTIONS);
    }

}
